//sturnfie-pov-zigzag.c for IMSAInterssion 2011
//   Originally written by Lucas Sturnfield (sturnfie)

/*              Overview

       This firmware createa a simple zigzag pattern.
       The firmware assumes that 8 LEDs are connected
       in sequence to PORTB.

*/



// This configures the device to operate at 1MHZ
#pragma config FOSC = INTIO67
#pragma config WDTEN = OFF, LVP = OFF
#include <p18f46K20.h>


void main (void) {
     // We will be shifting 0's in from a side to move the 1 (lit LED)
     char pattern = 0b00000001;
     char shiftDir = 0;	//1 RSHIFT, 0 LSHIFT

     // Set TRISB to configure PORTB as OUTPUTS
     TRISB = 0;

     // Configure the LATB register to set all PORTB outputs HIGH
     // Remember: We are grounding the LEDs into the PORT pins.
     // The LED will turn on when the pin LATB OUPUT is LOW
     LATB = 0xFF;

     while (1) {
           // Delay some cycles between each LED change
           Delay100TCYx(5);

           // We are at one end of the LED string, switch directions
           if(pattern == 0b1000000){
                // Change shift direction
                shiftDir = 1;
           }
           // We are at the other end, switch directions
           else if(pattern == 0b00000001){
                //Change shift direction
                shiftDir = 0;
           }

           if(shiftDir == 0){
                //Left shift in a 1
                pattern = pattern << 1;
           }
           else{
                //Right shift in a 1
                pattern = pattern >> 1;
           }


           // Set the IO pin Latches
           // We need to invert the pattern bit string since
           // the LEDs are wired to light when LATB is LOW
           LATB = ~pattern;
     }
}